using DevExpress.Xpo;
using SmartPeople.Framework.SecuritySpace;

namespace SmartPeople.Framework.Security.Providers.XPO
{
    public class XPSecuritySpace : SecuritySpaceBase
    {
        public XPSecuritySpace(string applicationName, string connectionString)
        {
            //base.Initialize(name, config);
            if (!Session.DefaultSession.IsConnected)
            {
                Session.DefaultSession.ConnectionString = connectionString;
                //Session.DefaultSession.AutoCreateOption = AutoCreateOption.None;
                //Session.DefaultSession.AutoCreateOption = AutoCreateOption.SchemaOnly;
            }
            Session.DefaultSession.BeginTransaction();
            XPSecurityApplicationProvider securityApplicationProvider = new XPSecurityApplicationProvider();
            //you pass the security application name to security space
            securityApplication = securityApplicationProvider.GetApplication(applicationName);
            if (securityApplication == null)
                securityApplication = securityApplicationProvider.CreateApplication(applicationName, "");
            Initialize();
            Session.DefaultSession.CommitTransaction();
        }

        private void Initialize()
        {
            securityUsers = new SecurityUsers(new XPSecurityUserProvider(securityApplication.Name));
            securityRoles = new SecurityRoles(new XPSecurityRoleProvider(securityApplication.GID, securityUsers));
            securitySecuredTypes = new SecuritySecuredTypes(new XPSecuritySecuredTypeProvider(securityApplication.GID));
            //TODO:Check if already there???

            ISecuritySecuredType securedFolder = SecuredTypes.GetSecuredType(BuiltInSecuredTypes.Folder);
            if (securedFolder == null)
            {
                securedFolder = securitySecuredTypes.CreateType(BuiltInSecuredTypes.Folder);
                securedFolder.Actions.CreateAction(BuiltInActions.List);
                securedFolder.Actions.CreateAction(BuiltInActions.Read);
                securedFolder.Actions.CreateAction(BuiltInActions.Create);
                securedFolder.Actions.CreateAction(BuiltInActions.Modify);
                securedFolder.Actions.CreateAction(BuiltInActions.Delete);

                securitySecuredTypes.UpdateSecuredType(securedFolder);
            }

            //Experimental !!!
            ISecuritySecuredType securedFile = SecuredTypes.GetSecuredType(BuiltInSecuredTypes.UnknownFileType);
            if (securedFile == null)
            {
                securedFile = SecuredTypes.CreateType(BuiltInSecuredTypes.UnknownFileType);
                securedFile.Actions.CreateAction(BuiltInActions.Read);
                securedFile.Actions.CreateAction(BuiltInActions.Create);
                securedFile.Actions.CreateAction(BuiltInActions.Modify);
                securedFile.Actions.CreateAction(BuiltInActions.Delete);

                securitySecuredTypes.UpdateSecuredType(securedFile);
            }

            securedDocuments =
                new SecuritySecuredDocuments(this, new XPSecuritySecuredDocumentsProvider(), securedFolder);
            securedDocuments.SetDefaultPathType(securedFolder);


            securityPermissions = new SecurityPermissions(new XPSecurityPermissionProvider());
            intializer = new Intializer();
        }

        private ISecurityApplication securityApplication;

        private ISecurityUser currentUser;

        private SecurityUsers securityUsers;

        private SecurityRoles securityRoles;

        private SecuritySecuredTypes securitySecuredTypes;

        private SecuritySecuredDocuments securedDocuments;

        private ISecurityPermissions securityPermissions;

        private Intializer intializer;

        #region ISecuritySpace Members

        public override ISecurityUser CurrentUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }


        public override SecurityUsers Users
        {
            get { return securityUsers; }
        }

        public override SecurityRoles Roles
        {
            get { return securityRoles; }
        }

        public override SecuritySecuredTypes SecuredTypes
        {
            get { return securitySecuredTypes; }
        }

        public override SecuritySecuredDocuments SecuredDocuments
        {
            get { return securedDocuments; }
        }

        public override ISecurityPermissions SecurityPermissions
        {
            get { return securityPermissions; }
        }

        public override Intializer Intializer
        {
            get { return intializer; }
            set { intializer = value; }
        }

        ///// <summary>
        ///// Checck if <see cref="ISecuritySpace.CurrentUser">CurrentUser</see> had the permission.
        ///// </summary>
        ///// <param name="objectPath"></param>
        ///// <param name="actionName"></param>
        //public override void Demand(string objectPath, string actionName)
        //{
        //    throw new NotImplementedException();
        //}

        //public override void Demand(string userNname, string objectPath, string actionName)
        //{
        //    throw new NotImplementedException();
        //}

        //public override void Demand(ISecurityUser user, string objectPath, string actionName)
        //{
        //    throw new NotImplementedException();
        //}

        #endregion
    }
}